var gulp = require('gulp'),
    uglify = require('gulp-uglify'),
    babel = require("gulp-babel"),
    browserify = require('gulp-browserify'),
    minifyCSS = require('gulp-minify-css'),
    sass = require('gulp-sass'),
    autoprefixer = require('gulp-autoprefixer'),
    rename = require('gulp-rename'),
    concat = require('gulp-concat'),
    imagemin = require('gulp-imagemin'),
    imageminJpegRecompress = require('imagemin-jpeg-recompress'),
    imageminOptipng = require('imagemin-optipng'),
    browserSync = require('browser-sync').create(),
    ghpages = require('gh-pages'),
    sftp = require('gulp-sftp');


gulp.task('script', function() {   
    gulp.src('src/js/*.js')
        .pipe(browserify({
            insertGlobals: true
        }))
        .pipe(babel({
            presets: ['es2015']
        }))
        .pipe(uglify())
        .pipe(rename({ suffix: '.min' }))
        .pipe(gulp.dest('dist/js'));
});

gulp.task('sass', function() {   
    gulp.src('src/sass/*.scss')
        .pipe(sass({
            outputStyle: 'compressed'
        }))
        .pipe(autoprefixer({
            browsers: ['last 2 versions', 'Safari >0', 'Explorer >0', 'Edge >0', 'Opera >0', 'Firefox >=20'], //last 2 versions- 主流浏览器的最新两个版本
            cascade: true, //是否美化属性值 默认：true 像这样：
            //-webkit-transform: rotate(45deg);
            //        transform: rotate(45deg);
            remove: true //是否去掉不必要的前缀 默认：true 
        }))
        .pipe(concat('main.css'))
        .pipe(minifyCSS())
        .pipe(rename({ suffix: '.min' }))
        .pipe(gulp.dest('dist/css'));
});

gulp.task('imgmin', function() { 
    var jpgmin = imageminJpegRecompress({
            accurate: true, //高精度模式
            quality: "high", //图像质量:low, medium, high and veryhigh;
            method: "smallfry", //网格优化:mpe, ssim, ms-ssim and smallfry;
            min: 70, //最低质量
            loops: 0, //循环尝试次数, 默认为6;
            progressive: false, //基线优化
            subsample: "default" //子采样:default, disable;        
        }),
        pngmin = imageminOptipng({
            optimizationLevel: 4 
        });    
    gulp.src('src/images/**/*')
        .pipe(imagemin({
            use: [jpgmin, pngmin]
        }))
        .pipe(gulp.dest('dist/images/'));
});

gulp.task('html', function() {
    gulp.src('src/**/*.html')
        .pipe(gulp.dest('dist'));
});

gulp.task('css', function() {
    gulp.src('src/**/*.css')
        .pipe(minifyCSS())
        .pipe(gulp.dest('dist'));
});

gulp.task('lib', function() {
    gulp.src('src/lib/css/*.css')
        .pipe(gulp.dest('dist/css'));
    gulp.src('src/lib/js/*.js')
        .pipe(gulp.dest('dist/js'));
    gulp.src('src/fonts/*.*')
        .pipe(gulp.dest('dist/fonts'));
});

gulp.task('server', function() {
    browserSync.init({
        port: 8011,
        server: {
            baseDir: ["dist", "dist/html"],
            index: "index.html"
        }
    });
});
gulp.task('dist', function() {
    ghpages.publish('dist', function(err) {});
});

gulp.task('watch', function() {
    gulp.watch('src/js/*.js', ['script']); 
    gulp.watch('src/sass/*.scss', ['sass']);
    gulp.watch('src/img/*.*', ['imgmin']);
    gulp.watch('src/**/*.html', ['html']);
    gulp.watch('dist/**/*.*').on('change', browserSync.reload);
});

gulp.task('build', ['script', 'css', 'sass', 'lib', 'imgmin', 'html']);
gulp.task('default', ['script', 'css', 'sass', 'lib', 'imgmin', 'html', 'server', 'watch']);